package com.boot.ys.mybatisplus.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.List;
import java.util.stream.Collectors;
import java.io.*;
import java.nio.file.*;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 描述：在实体类里面导入mybatisplus的@TableField注解的包
 *
 * @author: 杨升
 * @date: 2025-06-14 15:39
 * @Version 1.0
 */

public class JavaFileModifier {
    public static void main(String[] args) {
        printAllTablesWithComments(
                "jdbc:mysql://localhost:3306/springboot3txar?useSSL=false&serverTimezone=UTC&useInformationSchema=true",
                "root",
                "123456",
                "springboot3txar");

    }

    public static void printAllTablesWithComments(String dbUrl, String user, String password, String schemaName) {
        try {
            // 显式加载MySQL驱动类
            Class.forName("com.mysql.cj.jdbc.Driver");

            try (Connection conn = DriverManager.getConnection(dbUrl, user, password)) {
                DatabaseMetaData metaData = conn.getMetaData();

                // 打印表头
                System.out.println("+----------------------+----------------------+");
                System.out.printf("| %-20s | %-20s |%n", "表名", "表注释");
                System.out.println("+----------------------+----------------------+");

                // 获取所有表
                try (ResultSet tables = metaData.getTables(schemaName, null, "%", new String[]{"TABLE"})) {
                    while (tables.next()) {
                        String tableName = tables.getString("TABLE_NAME");
                        String remarks = tables.getString("REMARKS");

                        // 处理可能为null的注释
                        remarks = remarks != null ? remarks : "无注释";

                        // 格式化输出
                        System.out.printf("| %-20s | %-20s |%n", tableName, remarks);
                    }
                }
                System.out.println("+----------------------+----------------------+");

            }
        } catch (ClassNotFoundException e) {
            System.err.println("MySQL驱动未找到，请检查依赖配置");
        } catch (Exception e) {
            System.err.println("获取表信息失败: " + e.getMessage());
        }
    }


    // public static void main(String[] args) {
    //     String directoryPath = "C:\\Users\\Acer\\Desktop\\1\\springboot3txar\\src\\main\\java\\com\\entity";
    //
    //     try {
    //         modifyJavaFiles(directoryPath);
    //         System.out.println("文件修改完成！");
    //     } catch (IOException e) {
    //         System.err.println("处理文件时出错: " + e.getMessage());
    //         e.printStackTrace();
    //     }
    // }

    public static void modifyJavaFiles(String directoryPath) throws IOException {
        // 获取目录下所有.java文件
        List<File> javaFiles = Files.walk(Paths.get(directoryPath))
                .filter(Files::isRegularFile)
                .filter(path -> path.toString().endsWith(".java"))
                .map(Path::toFile)
                .collect(Collectors.toList());

        for (File file : javaFiles) {
            processFile(file);
        }
    }

    private static void processFile(File file) throws IOException {
        // 读取文件内容
        List<String> lines = Files.readAllLines(file.toPath());
        boolean modified = false;

        // 遍历每一行
        for (int i = 0; i < lines.size(); i++) {
            String line = lines.get(i).trim();
            // 检查是否包含目标package声明
            if (line.equals("package com.entity.model;")) {
                // 检查下一行是否已经是我们要插入的import
                if (i + 1 < lines.size() && !lines.get(i + 1).trim().equals("import com.baomidou.mybatisplus.annotations.TableField;")) {
                    // 在package声明后插入import
                    lines.add(i + 1, "import com.baomidou.mybatisplus.annotations.TableField;");
                    modified = true;
                    // 跳过刚插入的行
                    i++;
                } else if (i + 1 >= lines.size()) {
                    // 如果是最后一行，直接添加
                    lines.add("import com.baomidou.mybatisplus.annotations.TableField;");
                    modified = true;
                }
            }
        }

        // 如果文件被修改，则写入新内容
        if (modified) {
            Files.write(file.toPath(), lines);
            System.out.println("已修改文件: " + file.getAbsolutePath());
        }
    }

}
